package com.heima.file.controller;

import com.heima.file.config.FastDFSClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/upload")
public class FileController {
    @Autowired
    FastDFSClientUtil fastDFSClientUtil;
    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @PostMapping("/file")
    public String uploadFile(MultipartFile file){
        try {
            if(file == null){
                throw new RuntimeException("文件不存在");
            }
            String originalFilename = file.getOriginalFilename();
            if(StringUtils.isEmpty(originalFilename)){
                throw new RuntimeException("文件不存在");
            }
            String s = fastDFSClientUtil.uploadFile(file);
            return fileServerUrl+s;
        }catch (Exception e){
            e.printStackTrace();
        }
        return "上传失败";
    }

    @GetMapping("/del")
    public String delete(String file){
        try {
            fastDFSClientUtil.delFile(file);
            return "删除成功";
        }catch (Exception e){
            e.printStackTrace();
            return "删除失败";
        }
    }
}
